/**
 * Created by WYH on 16/12/30.
 */
$(document).ready(function () {
    JudgeBrowser.changeTopBarTextSizeInIE();
    if (JudgeLogin.isLogin()) {
        setupManagerName();
        bindClickEvent();
        InputCheck.checkInputValueIsNumber(".oldPhoneNumer");
        InputCheck.checkInputValueIsNumber(".newPhoneNumer");
        InputCheck.checkInputValueIsNumber(".confirmNewPhoneNumer");
        JudgeLogin.bindSignOut(".signOut");
    }
});

function setupManagerName() {
    Api.fetchCompanyDetailInfo(function (result) {
        $(".managerBaseInfo").text("管理员：" + result.data.companyName);
    }, function (error) {
        alert("获取管理员名字失败");
    })
}

function bindClickEvent() {
    clickGetVerifyCode();
    clickConfirmChangeButton();
}

function clickGetVerifyCode() {
    $(".getVerifyCode").click(function () {
        var oldPhoneNumer = $(".oldPhoneNumer").val();
        if (oldPhoneNumer == "") {
            alert("请输入手机号");
        } else if(!InputCheck.checkInputValueIsPhone(".oldPhoneNumer")) {
            alert("手机号格式不对");
        } else {
            //post  oldPhoneNumer
            sendVerifyCode(oldPhoneNumer);
        }
    });

}

function sendVerifyCode(phone) {
    Api.fetchVerificationCode(phone, function (result) {
        if (result.code == 202) {
            startTimeInterVal();
        } else {
            alert("发送验证码失败");
        }
    }, function (error) {
        alert("发送验证码失败");
    })
}

function clickConfirmChangeButton() {
    $(".confirmChangeButton").click(function () {
        var oldPhone = $(".oldPhoneNumer").val();
        var newPhone = $(".newPhoneNumer").val();
        var verifyCode = $(".messageVerifyCode").val();
        var confirmPhone = $(".confirmNewPhoneNumer").val();
        if (judgePhoneIsValid(oldPhone, newPhone, confirmPhone, verifyCode)) {
            //post  oldPhone newPhone code
            changePhone(newPhone, verifyCode);
        }
    })
}

function changePhone(newPhone, verifyCode) {
    Api.changeCompanyPhone(newPhone, verifyCode, function (result) {
        handleChangePhoneResult(result);
    }, function (error) {
        console.log(error);
    })
}

function handleChangePhoneResult(result) {
     if (result.code == 200) {
         alert("修改成功");
         window.location.href = "../../html/company/companyInfo.html";
     } else if (result.code == 400) {
         alert('手机格式错误');
     } else if (result.code == 401) {
         alert('验证码错误');
     } else  if(result.code == 404) {
         alert('未找到用户敏感');
     } else if (result.code == 409) {
         alert('用户名已经存在');
     }
}


function startTimeInterVal() {
    var time = 60;
    $(".getVerifyCode").attr("disabled", true);
    var timeInterval = setInterval(function () {
        $(".getVerifyCode").val(time == -1 ? "获取验证码" : time);
        if (time == -1) {
            window.clearInterval(timeInterval);
            $(".getVerifyCode").attr("disabled", false);
        }
        time--;
    }, 1000);
}

function judgePhoneIsValid(oldPhone, newPhone, confirmPhone, verifyCode) {
    if (oldPhone == "" || newPhone == "" || confirmPhone == "" || verifyCode == "") {
        alert("请填写完整信息");
        return false;
    } else if (newPhone != confirmPhone){
        alert("两次输入的手机号码不相同");
        return false
    } else {
        return true;
    }
}
